package structure.queue;

/**
 * 数组队列
 */
public class ArrayQueue {
    private int maxSize; //数组最大的容量
    private int front; //队列头
    private int rear; //队列尾
    private int[] arr; //模拟队列

    public static void main(String[] args) {
        ArrayQueue arrayQueue = new ArrayQueue(3);

        arrayQueue.addQueue(1);
        arrayQueue.addQueue(2);

        arrayQueue.showQueue();
    }

    public ArrayQueue( int arrMaxSize ) {
        this.maxSize = arrMaxSize;
        arr = new int[this.maxSize];
        this.front = -1;
        this.rear = -1;
    }

    //判断对列是否已满
    public boolean isFull() {
        return rear == maxSize - 1;
    }

    //判断队列是否为空
    public boolean isEmpty() {
        return this.rear == this.front;
    }

    //添加数据到队列
    public void addQueue(int n) {
        if (isFull()) {
            System.out.println("已满");
            return;
        }
        this.rear++;
        this.arr[this.rear] = n;
    }

    //获取队列的数据，出队列
    public int getQueue() {
        if ( this.isFull() ) {
            throw new RuntimeException("队列空");
        }
        this.front++;
        return this.arr[this.front];
    }

    //显示队列数据
    public void showQueue() {
        if ( this.isFull() ) {
            throw new RuntimeException("队列空");
        }
        for( int i=0; i<this.arr.length;i++ ) {
            System.out.printf( "arr[%d]=%d\n", i, arr[i] );
        }
    }

    //显示队列的头
    public int headQueue() {
        if ( this.isFull() ) {
            throw new RuntimeException("队列空");
        }
        return arr[front + 1];
    }
}
